import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import remove from '@dfeidao/atom-web/local/remove';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import s_get from '@dfeidao/atom-web/storage/get';

import p001 from './p001';
import p002 from './p002';
import p003 from './p003';
////////////////////////////////////////////////////////////////////////////////


// 表体
export default async function a015(fd: IFeidaoAiBrowserComponent) {
	// render(fd, list_title_data, P06, 'P06', 'inner');
	const criteria = get(fd, 'criteria') as { productid: string };
	// console.log(criteria, 222222)
	const page_no = get(fd, 'page_no') as number;
	const page_size = get(fd, 'page_size') as number;
	// console.log(page_no, page_size);
	// const user = await whoami(get_user_info);
	// console.log(user)

	const productid = await s_get('productid');
	const sort = get(fd, 'sort');

	criteria.productid = productid;
	// console.log(criteria)
	const query = {
		filter: criteria,
		productid,
		sort,
		page_no,
		page_size
	};
	const res = await nodejs<{ dataArray: object[], page_no: number, page_size: number, count: number }>('consumer/zj-000001/s004', query);
	// console.log(res)
	fd.data.node.querySelector<HTMLInputElement>('[data-feidao-id="checkall"]').checked = false;
	if (res.count === 0) {
		render(fd, '', p001, 'p001', 'inner');
		fd.data.node.querySelector('[data-feidao-presentation="p003"]').innerHTML = '';
		render(fd, '', p002, 'p002', 'inner');
	} else {
		const tb = res.dataArray;
		const data = tb.map((v) => {
			return { ...v, v: JSON.stringify(v) };
		});
		if (res.count >= page_size) {
			res.page_no = page_no;
			res.page_size = page_size;
			render(fd, res, p003, 'p003', 'inner');
		} else {
			fd.data.node.querySelector('[data-feidao-presentation="p003"]').innerHTML = '';
		}
		render(fd, data, p001, 'p001', 'inner');
		fd.data.node.querySelector('[data-feidao-presentation="p002"]').innerHTML = '';
	}

	remove(fd, 'wk_id');
	remove(fd, 'wk_code');


}
